package kgc.crm.controller;

import com.alibaba.druid.sql.visitor.functions.If;
import kgc.crm.annotation.UserPower;
import kgc.crm.annotation.VerificationToken;
import kgc.crm.enums.PowerEnum;
import kgc.crm.model.Menu;
import kgc.crm.service.IMenuService;
import kgc.crm.util.CommonResult;
import kgc.crm.util.EmptyChecker;
import kgc.crm.util.RequestUtil;
import kgc.crm.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//用户相关操作
@RestController
@CrossOrigin
@RequestMapping("user")
public class UserController {
    @Autowired
    private IMenuService menuService;


//    public static void main(String[] args) {
//        ArrayList list = new ArrayList();
//        String S= "2";
//        list.add(1);
//       if (EmptyChecker.isEmpty(list)){
//           System.out.println("空");
//       }else {
//           System.out.println("不为空");
//       };
//        if (EmptyChecker.isEmpty(S)) {
//            System.out.println("字符串为空");
//        }else
//            System.out.println("不为空");
//    }
    //10
    @UserPower(power = PowerEnum.INDEX)
    @VerificationToken  //token注解验证
    @GetMapping("menu/list/by/id")
    public ResponseEntity getMenuListById(HttpServletRequest request) {
        String apiToken = RequestUtil.getApiToken(request);//获取一个apitoken
        List<Menu> list = menuService.getMenuListByUserId(apiToken);
        if (EmptyChecker.isEmpty(list)) {
            return new ResponseEntity(new CommonResult(500,"error",null), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return ResultUtil.success(list);
    }
}
